<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class LibImageThumb {
	var $srcfile = '';
	var $targetfile = '';
	var $imagecreatefromfunc = '';
	var $imagefunc = '';
	var $attach = array();
	var $attachinfo = '';
	var $animatedgif = 0;
	
	var $imagelib = 'gd2';
	var $imageimpath = '';
	
	var $defaulttargetfile = 'image_tmp.jpg';
	
	var $thumbstatus = '2';//from 0, 1 2
	
	var $watermarktext = '';
	var $watermarktype = '';
	var $watermarktrans = '';
	var $watermarkquality = '';
	var $watermarkminwidth = 0;
	var $watermarkminheight = 0;
	var $watermarkstatus = '1';//from 1 to 9
	//		xx	xx	xx
	//		xx	xx	xx
	//		xx	xx	xx
	//
	
	//constructor
	function LibImageThumb($srcfile, $targetfile, $attach = array()) {
		$this->defaulttargetfile = CONFIG_PATH . CONFIG_DIR_IMAGES . $this->defaulttargetfile;
		
		$this->srcfile = $srcfile;
		$this->targetfile = $targetfile;
		$this->attach = $attach;
		$this->attachinfo = @getimagesize($targetfile);
		
		if( $this->imagelib == 'gd2' ) {
			switch($this->attachinfo['mime'] ) {
				case 'image/jpeg':
					$this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
					$this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
					break;
				case 'image/gif':
					$this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
					$this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
					break;
				case 'image/png':
					$this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng': '';
					$this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
					break;
			}
		} else {
			$this->imagecreatefromfunc = true;
			$this->imagefunc = true;
		}
		
		$this->attach['size'] = empty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];
		if( $this->attachinfo['mime'] == 'image/gif' ) {
			$fp = fopen($targetfile, 'rb');
			$targetfilecontent = fread($fp, $this->attach['size']);
			fclose($fp);
			$this->animatedgif = (strpos($targetfilecontent, 'NETSCAPE2.0') === false) ? 0 : 1;
		}
	}
	
	function thumb($thumbwidth, $thumbheight, $preview = 0) {
		if( $this->imagelib == 'im' && $this->imageimpath ) {
			$this->_thumb_im($thumbwidth, $thumbheight, $preview);
		} else {
			$this->_thumb_gd($thumbwidth, $thumbheight, $preview);
		}
		if( $this->thumbstatus == 2 && $this->watermarkstatus ) {
			$this->LibImageThumb($this->srcfile, $this->targetfile, $this->attach);
			$this->attach['size'] = filesize($this->targetfile);
		}
	}
	
	function watermark($preview = 0) {
		if( $watermarkminwidth && $this->attachinfo['0'] < $watermarkminwidth && $watermarkminheight && $this->attachinfo['1'] <= $watermarkminheight ) {
			//
		} elseif( $this->watermarktype == 2 && (!file_exists($watermarktext['fontpath']) || !is_file($watermarktext['fontpath'])) ) {
			//
		} else {
			if( $this->imagelib == 'im' && $this->imageimpath ) {
				$this->_watermark_im($preivew);
			} else {
				$this->_watermark_gd($preview);
			}
		}
	}
	
	//private
	function _thumb_gd($thumbwidth, $thumbheight, $preview = 0) {
		if( $this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg') ) {
			$imagecreatefromfunc = $this->imagecreatefromfunc;
			$imagefunc = ($this->thumbstatus == 1) ? 'imagejpeg' : $this->imagefunc;
			
			list($img_w, $img_h) = $this->attachinfo;
			
			if( !$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight) ) {
				//
				$attach_photo = $imagecreatefromfunc($this->targetfile);
				$x_ratio = $thumbwidth / $img_w;
				$y_ratio = $thumbheight / $img_h;
				if( ($x_ratio * $img_h) < $thumbheight ) {
					$thumb['height'] = ceil($x_ratio * $img_h);
					$thumb['width'] = $thumbwidth;
				} else {
					$thumb['width'] = ceil($y_ratio * $img_w);
					$thumb['height'] = $thumbheight;
				}
				//
				if( $preview == 0 ) {
					if( $this->thumbstatus == 1 ) {
						$targetfile = $this->targetfile . 'thumb.jpg';
					} else {
						$targetfile = $this->targetfile;
					}
				} else {
					$targetfile = $this->defaulttargetfile;
				}
				//
				$thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
				imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $img_w, $img_h);
				if( $this->attachinfo['mime'] == 'image/jpeg' ) {
					$imagefunc($thumb_photo, $targetfile, 100);
				} else {
					$imagefunc($thumb_photo, $targetfile);
				}
				$this->attach['thumb'] = ($this->thumbstatus) == 1 ? 1 : 0;
			}
		}
	}
	
	function _watermark_gd($preview = 0) {
		if( $this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge') ) {
			$imagecreatefromfunc = $this->imagecreatefromfunc;
			$imagefunc = $this->imagefunc;
			list($img_w, $img_h) = $this->attachinfo;
			if( $this->watermarktype < 2 ) {
				$watermark_file = ($this->watermarktype == 1) ? 'watermark.png' : 'watermark.gif';
				$watermarkinfo = @getimagesize($watermark_file);
				$watermark_logo = ($this->watermarktype == 1) ? @imagecreatefrompng($watermark_file) : imagecreatefromgif( $watermark_file);
				if( !$watermark_logo ) {
					return;
				}
				list($logo_w, $logo_h) = $watermarkinfo;
			} else {
				$watermarktextcvt = pack("H*", $this->watermarktext['text']);
				$box = imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'], $this->watermarktextcvt);
				$logo_h = max($box['1'], $box['3']) - min($box['5'], $box['7']);
				$logo_w = max($box['2'], $box['4']) - min($box['0'], $box['7']);
				$ax = min($box['0'], $box['6']) * -1;
				$ay = min($box['5'], $box['7']) * -1;
			}
			$wmwidth = $img_w - $logo_w;
			$wmheight = $img_h - $logo_h;
			
			if( ($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif ) {
				switch($this->watermarkstatus) {
					case 1:
						$x = +5;
						$y = +5;
						break;
					case 2:
						$x = ($img_w - $logo_w) / 2;
						$y = +5;
						break;
					case 3:
						$x = $img_w - $logo_w - 5;
						$y = +5;
						break;
					case 4:
						$x = +5;
						$y = ($img_h - $logo_h) / 2;
						break;
					case 5:
						$x = ($img_w - $logo_w) / 2;
						$y = ($img_h - $logo_h) / 2;
						break;
					case 6:
						$x = $img_w - $logo_w;
						$y = ($img_h - $logo_h) / 2;
						break;
					case 7:
						$x = +5;
						$y = $img_h - $logo_h - 5;
						break;
					case 8:
						$x = ($img_w - $logo_w) / 2;
						$y = $img_h - $logo_h - 5;
						break;
					case 9:
						$x = $img_w - $logo_w - 5;
						$y = $img_h - $logo_h - 5;
						break;
				}
				//
				$dst_photo = imagecreatetruecolor($img_w, $img_h);
				$target_photo = @$imagecreatefromfunc($this->targetfile);
				imagecopy($dest_photo, $target_photo, 0, 0, 0, 0, $img_w, $img_h);
				
				if( $this->watermarktype == 1 ) {
					imagecopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h);
				} elseif( $this->watermarktype == 2 ) {
					if( ($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor'] ) {
						$shadowcolorrgb = explode(',', $watermarktext['shadowcolor']);
						$shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
						imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax + $watermarktext['shadowx'], $y + $ay + $watermarktext['shadowy'], $shadowcolor, $watermarktext['fontpath'], $watermarktextcvt);
					}
					$colorrgb = explode(',', $watermarktext['color']);
					$color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
					imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax, $y + $ay, $color, $watermarktext['fontpath'], $watermarktextcvt);
				} else {
					imageAlphaBlending($watermark_logo, true);
					imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $watermarktrans);
				}
				
				//
				$targetfile = ($preview == 0) ? $this->targetfile : $this->watermarkdefaultfile;
				if( $this->attachinfo['mime'] == 'image/jpeg' ) {
					$imagefunc($dst_photo, $targetfile, $this->watermarkquality);
				} else {
					$imagefunc($dst_photo, $targetfile);
				}
				$this->attach['size'] = filesize($this->targetfile);
			}
		}
	}
	
	function _thumb_im($thumbwidth, $thumbheight, $preview = 0) {
		if( $this->thumbstatus ) {
			list($img_w, $img_h) = $this->attachinfo;
			if( $preview == 0 ) {
				if( $this->thumbstatus == 1 ) {
					$targetfile = $this->targetfile . 'thumb.jpg';
				} else {
					$targetfile = $this->targetfile;
				}
			} else {
				$targetfile = $this->defaulttargetfile;
			}
			//
			if( !$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumb_height) ) {
				$exec_str = $this->imageimpath . 'convert -geometry ' . $thumbwidth . 'x' . $thumbheight . ' ' . $this->targetfile . " " . $targetfile;
				@exec($exec_str, $output, $return);
				if( empty($return) && empty($output) ) {
					$this->attach['thumb'] = ($this->thumbstatus == 1) ? 1 : 0;
				}
			
			}
		}
	}
	
	function _watermark_im($preview = 0) {
		switch($this->watermarkstatus) {
			case 1:
				$gravity = 'NorthWest';
				break;
			case 2:
				$gravity = 'North';
				break;
			case 3:
				$gravity = 'NorthEast';
				break;
			case 4:
				$gravity = 'West';
				break;
			case 5:
				$gravity = 'Center';
				break;
			case 6:
				$gravity = 'East';
				break;
			case 7:
				$gravity = 'SouthWest';
				break;
			case 8:
				$gravity = 'South';
				break;
			case 9:
				$gravity = 'SouthEast';
				break;
		}
		
		$targetfile = !$preview ? $this->targetfile : $this->watermarkdefault;
		if( $this->watermarktype < 2 ) {
			$watermark_file = ($this->watermarktype == 1) ? $this->watermarkpng : $this->watermarkgif;
			$exec_str = $this->imageimpath . '/composite'.
				($this->watermarktype != 1 && $this->watermarktrans != '100' ? ' -watermark '. $this->watermarktrans . '%' : '') .
				' -gravity ' . $this->gravity.
				' ' . $watermark_file . ' ' . $this->targetfile . ' ' . $targetfile;
		} else {
			$this->watermarktextcvt = str_replace(array("\n", "\r", "'"), array('', '', '\''), pack("H*", $this->watermarktext['text']));
			$this->watermarktext['angle'] = - $this->watermarktext['angle'];
			$translate = $this->watermarktext['translatex'] || $this->watermarktext['translatey'] ? ' translate ' . $this->watermarktext['translatex'] . ',' . $this->watermarktext['translatey'] : '';
			$skewX = $this->watermarktext['skewx'] ? ' skewX '.$watermarktext['skewx'] : '';
			$skewY = $this->watermarktext['skewy'] ? ' skewY '.$watermarktext['skewy'] : '';
			$exec_str = $this->imageimpath . '/convert'.
				' -font "' . $this->watermarktext['fontpath'] . '"' .
				' -pointsize ' . $this->watermarktext['size'] . 
				(($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor'] ?
					' -fill "rgb(' . $this->watermarktext['shadowcolor'].')"'.
					' -draw "'.
						' gravity ' . $gravity . $translate . $skewX . $skewY.
						' rotate ' . $this->watermarktext['angle'] . 
						' text ' . $this->watermarktext['shadowx'] . ',' . $this->watermarktext['shadowy'].' \'' . $this->watermarktextcvt . '\'"' : '') . 
				' -fill "rgb(' . $this->watermarktext['color'] . ')"' .
				' -draw "'.
					' gravity ' . $gravity . $translate . $skewX . $skewY . 
					' rotate ' . $this->watermarktext['angle'] . 
					' text 0,0 \'' . $this->watermarktextcvt . '\'"' . 
				' ' . $this->targetfile . ' '. $targetfile;
		}
		@exec($exec_str, $output, $return);
		if( empty($return) && empty($output) ) {
			$this->attach['size'] = filesize($this->targetfile);
		}
	}
}
//
?>